package action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import bean.Teacher;
import service.LoginService;
import tools.MD5;

@Controller("LoginManage")
@Scope("prototype")
public class LoginManageAction {
	@Resource LoginService loginService;

//--------------------------get和set方法------------------------	
	
	private Teacher teacher;
	private String tusername;
	private String tpassword;
	private int tid;
	private Map<String, Object> loginJson;		//回传的数据

	
	public int getTid() {
		return tid;
	}

	public void setTid(int tid) {
		this.tid = tid;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public String getTusername() {
		return tusername;
	}

	public void setTusername(String tusername) {
		this.tusername = tusername;
	}

	public String getTpassword() {
		return tpassword;
	}

	public void setTpassword(String tpassword) {
		this.tpassword = tpassword;
	}

	public Map<String, Object> getLoginJson() {
		return loginJson;
	}

	public void setLoginJson(Map<String, Object> loginJson) {
		this.loginJson = loginJson;
	}

	//====================================Action=========================================
	
	/**
	 * 电脑端登录
	 * @return relogin/mainpage
	 */
	public String login()
	{
        Teacher teacher = loginService.findTeacherByUsername(getTusername());
		if(teacher == null)						//账户不存在
		{
			ServletActionContext.getRequest().getSession().setAttribute("loginresult", "wrongname");
			//ActionContext.getContext().put("result", "wrongname");
			return "relogin";
		}
		//将输入的密码进行比对
		if(!teacher.getTpassword().equals(MD5.Encrypt(getTpassword().trim())))
		{
			ServletActionContext.getRequest().getSession().setAttribute("loginresult", "wrongpwd");
			//ActionContext.getContext().put("result", "wrongpwd");
			return "relogin";		
		}
		ServletActionContext.getRequest().getSession().setAttribute("loginresult", "");
		ServletActionContext.getRequest().getSession().setAttribute("tid", teacher.getId());
		ServletActionContext.getRequest().getSession().setAttribute("username", getTusername());
		return "mainpage";
	}
	
	/**
	 * 电脑端登出 
	 * @return logoutfinish
	 */
	public String logout()
	{		
		//int tid = (int)ServletActionContext.getRequest().getSession().getAttribute("tid");
		//Teacher teacher = loginService.findTeacherById(tid);
		//teacher.setTlogout_time(new Date());
	//	ServletActionContext.getRequest().getSession().invalidate();		//销毁session
		ServletActionContext.getRequest().getSession().removeAttribute("loginresult");
		ServletActionContext.getRequest().getSession().removeAttribute("tid");
		ServletActionContext.getRequest().getSession().removeAttribute("username");
		return "relogin";
	}
	
	//==================安卓端=====================
	
	/**
	 * 登录
	 * @return loginfinish
	 */
	public String adlogin()
	{
		Map<String, Object> map = new HashMap<String, Object>();
        Teacher teacher = loginService.findTeacherByUsername(getTusername());
		if(teacher == null)						//账户不存在
		{
			System.out.println("账号不存在");
			map.put("result", "wrongname");					
			this.setLoginJson(map);
			return "loginfinish";
		}
		//将输入的密码进行比对
		if(!teacher.getTpassword().equals(getTpassword().trim()))
		{
			System.out.println("密码错误");
			map.put("result", "wrongpassword");				//密码错误
			this.setLoginJson(map);
			return "loginfinish";		
		}
		map.put("tid", teacher.getId());				//向安卓端返回教师id
		map.put("result", "success");
		this.setLoginJson(map);
		teacher.setTlogin_time(new Date());				//更新最后一次登录时间
		loginService.updateTeacher(teacher);
		System.out.println("登陆成功");
		return "loginfinish";
	}

	/**
	 * 注册
	 * @return registerfinish
	 */
	public String adregister()
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", loginService.addTeacher(teacher));
		this.setLoginJson(map);
		System.out.println("注册结束");
		return "registerfinish";
	}
	
	/**
	 * 登出
	 * @return logoutfinish
	 */
	public String adlogout()
	{		
		Map<String, Object> map = new HashMap<String, Object>();
		Teacher teacher = loginService.findTeacherById(getTid());
		if(teacher == null)
		{
			map.put("result", "fail");
			this.setLoginJson(map);
			return "logoutfinish";
		}
		
		teacher.setTlogout_time(new Date());
		loginService.updateTeacher(teacher);
		map.put("result", "success");
		this.setLoginJson(map);
		return "logoutfinish";
	}
	
}